package com.strange.mdl.module.mdl.service.dailydharma;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.strange.mdl.module.mdl.dal.dataobject.dailydharma.DailydharmaUsersDO;
import com.strange.mdl.module.mdl.dal.dataobject.debatehall.DebatehallDO;
import com.strange.mdl.module.mdl.dal.dataobject.debatehall.DebatehallUserDO;
import com.strange.mdl.module.mdl.dal.mysql.dailydharma.DailydharmaUsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.strange.mdl.module.mdl.controller.admin.dailydharma.vo.*;
import com.strange.mdl.module.mdl.dal.dataobject.dailydharma.DailydharmaDO;
import com.strange.mdl.framework.common.pojo.PageResult;
import com.strange.mdl.framework.common.util.object.BeanUtils;

import com.strange.mdl.module.mdl.dal.mysql.dailydharma.DailydharmaMapper;

import static com.strange.mdl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.strange.mdl.framework.common.util.collection.CollectionUtils.convertList;
import static com.strange.mdl.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static com.strange.mdl.module.mdl.enums.ErrorCodeConstants.*;

/**
 * 今日弘法 Service 实现类
 *
 * @author 密谛灵总控
 */
@Service
@Validated
public class DailydharmaServiceImpl implements DailydharmaService {

    @Resource
    private DailydharmaMapper dailydharmaMapper;

    @Autowired
    private DailydharmaUsersMapper dailydharmaUsersMapper;

    @Override
    public Long createDailydharma(DailydharmaSaveReqVO createReqVO) {
        // 插入
        DailydharmaDO dailydharma = BeanUtils.toBean(createReqVO, DailydharmaDO.class);
        dailydharmaMapper.insert(dailydharma);
        // 返回
        return dailydharma.getId();
    }

    @Override
    public void updateDailydharma(DailydharmaSaveReqVO updateReqVO) {
        // 校验存在
        validateDailydharmaExists(updateReqVO.getId());
        // 更新
        DailydharmaDO updateObj = BeanUtils.toBean(updateReqVO, DailydharmaDO.class);
        dailydharmaMapper.updateById(updateObj);
    }

    @Override
    public void deleteDailydharma(Long id) {
        // 校验存在
        validateDailydharmaExists(id);
        // 删除
        dailydharmaMapper.deleteById(id);
    }

    @Override
        public void deleteDailydharmaListByIds(List<Long> ids) {
        // 校验存在
        validateDailydharmaExists(ids);
        // 删除
        dailydharmaMapper.deleteByIds(ids);
        }

    private void validateDailydharmaExists(List<Long> ids) {
        List<DailydharmaDO> list = dailydharmaMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(DAILYDHARMA_NOT_EXISTS);
        }
    }

    private void validateDailydharmaExists(Long id) {
        if (dailydharmaMapper.selectById(id) == null) {
            throw exception(DAILYDHARMA_NOT_EXISTS);
        }
    }

    @Override
    public DailydharmaDO getDailydharma(Long id, Long userId) {
        return dailydharmaMapper.selectDailyDharmaWithUserInfo(id, userId);
    }

    @Override
    public PageResult<DailydharmaDO> getDailydharmaPage(DailydharmaPageReqVO pageReqVO) {
        LambdaQueryWrapper<DailydharmaDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(pageReqVO.getId() != null, DailydharmaDO::getId, pageReqVO.getId());
        wrapper.eq(pageReqVO.getStatus() != null, DailydharmaDO::getStatus, pageReqVO.getStatus());
        wrapper.gt(pageReqVO.getCreateTime() != null, DailydharmaDO::getCreateTime, pageReqVO.getCreateTime());
        wrapper.like(StrUtil.isNotEmpty(pageReqVO.getTitle()), DailydharmaDO::getTitle, pageReqVO.getTitle());
        PageResult<DailydharmaDO> pageResult = dailydharmaMapper.selectPage(pageReqVO, null, wrapper);

        Long loginUserId = getLoginUserId();
        List<DailydharmaDO> list = pageResult.getList();
        if (loginUserId != null) {
            for (DailydharmaDO dailydharmaDO : list) {
                DailydharmaDO duserDO = dailydharmaMapper.selectDailydharmaDOUsers(dailydharmaDO.getId(), loginUserId);
                if (duserDO != null) {
                    dailydharmaDO.setIsLike(duserDO.getIsLike());
                    dailydharmaDO.setIsCollect(duserDO.getIsCollect());
                    dailydharmaDO.setIsShare(duserDO.getIsShare());
                    dailydharmaDO.setIsMessage(duserDO.getIsMessage());
                }
            }
        }
        return pageResult;
    }

    @Override
    public DailydharmaDO operate(Long userId, Long id, String tag, Integer status) {
        LambdaQueryWrapper<DailydharmaUsersDO> dailydharmaUsersDOLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dailydharmaUsersDOLambdaQueryWrapper.eq(DailydharmaUsersDO::getDailydharmaId, id);
        dailydharmaUsersDOLambdaQueryWrapper.eq(DailydharmaUsersDO::getUserId, userId);
        DailydharmaUsersDO dailydharmaUsersDO = dailydharmaUsersMapper.selectOne(dailydharmaUsersDOLambdaQueryWrapper);
        if (dailydharmaUsersDO != null) {
            if ("isLike".equals(tag)) {
                dailydharmaUsersDO.setIsLike(status);
            }
            if ("isCollect".equals(tag)) {
                dailydharmaUsersDO.setIsCollect(status);
            }
//            if ("isShare".equals(tag)) {
//                dailydharmaUsersDO.setIsShare(status);
//            }
//            if ("isMessage".equals(tag)) {
//                dailydharmaUsersDO.setIsMessage(status);
//            }
            dailydharmaUsersMapper.update(dailydharmaUsersDO, new LambdaQueryWrapper<DailydharmaUsersDO>().eq(DailydharmaUsersDO::getDailydharmaId, id).eq(DailydharmaUsersDO::getUserId, userId));
        } else {
            dailydharmaUsersDO = new DailydharmaUsersDO();
            if ("isLike".equals(tag)) {
                dailydharmaUsersDO.setIsLike(status);
            }
            if ("isCollect".equals(tag)) {
                dailydharmaUsersDO.setIsCollect(status);
            }
            if ("isShare".equals(tag)) {
                dailydharmaUsersDO.setIsShare(status);
            }
            if ("isMessage".equals(tag)) {
                dailydharmaUsersDO.setIsMessage(status);
            }
            dailydharmaUsersDO.setDailydharmaId(id);
            dailydharmaUsersDO.setUserId(userId);
            dailydharmaUsersMapper.insert(dailydharmaUsersDO);
        }

        DailydharmaDO dailydharma = getDailydharma(id, userId);

        return dailydharma;
    }


}